package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    // @Override
    // public void add(ShoppingCartDTO dto) {
    //     //判断该购物车是否存在
    //     ShoppingCart shoppingCart = new ShoppingCart();
    //     BeanUtils.copyProperties(dto,shoppingCart);
    //     shoppingCart.setUserId(BaseContext.getCurrentId());
    //     List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
    //     if (shoppingCartList != null && !shoppingCartList.isEmpty()) {
    //         //增加数量
    //         ShoppingCart cart = shoppingCartList.get(0);
    //         Integer number = cart.getNumber() + 1;
    //         cart.setNumber(number);
    //         shoppingCartMapper.updateNumberById(cart);
    //     } else {
    //         //新增购物车
    //         if (dto.getDishId() != null) {
    //             //菜品
    //             Dish dish = dishMapper.getDishById(dto.getDishId());
    //             ShoppingCart build = ShoppingCart.builder().name(dish.getName()).image(dish.getImage()).userId(BaseContext.getCurrentId())
    //                     .dishId(dto.getDishId()).dishFlavor(dto.getDishFlavor()).number(1).amount(dish.getPrice())
    //                     .createTime(LocalDateTime.now()).build();
    //             shoppingCartMapper.insert(build);
    //         } else {
    //             //套餐
    //             Setmeal setmeal = setmealMapper.getSetmealById(dto.getSetmealId());
    //             ShoppingCart build = ShoppingCart.builder().name(setmeal.getName()).image(setmeal.getImage()).userId(BaseContext.getCurrentId())
    //                     .setmealId(dto.getSetmealId()).number(1).amount(setmeal.getPrice())
    //                     .createTime(LocalDateTime.now()).build();
    //             shoppingCartMapper.insert(build);
    //         }
    //
    //     }
    // }

    @Override
    public void add(ShoppingCartDTO dto) {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        BeanUtils.copyProperties(dto, shoppingCart);
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);
        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            // 如果存在，则在原来数量上+1
            ShoppingCart cart = shoppingCartList.get(0);
            Integer number = cart.getNumber() + 1;
            cart.setNumber(number);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            // 如果不存在，则新增，初始数量是1
            if (dto.getDishId() != null) {
                // 菜品
                Dish dish = dishMapper.getDishById(dto.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 套餐
                Setmeal setmeal = setmealMapper.getSetmealById(dto.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart build = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shoppingCartMapper.list(build);
    }

    @Override
    public void clean() {
        shoppingCartMapper.clean(BaseContext.getCurrentId());
    }

    @Override
    public void sub(ShoppingCartDTO dto) {
        ShoppingCart cart = new ShoppingCart();
        BeanUtils.copyProperties(dto, cart);
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.list(cart);
        if (shoppingCarts == null || shoppingCarts.size() == 0) {
            throw new ShoppingCartBusinessException(MessageConstant.SHOPPING_CART_IS_NULL);
        }
        ShoppingCart shoppingCart = shoppingCarts.get(0);
        shoppingCart.setNumber(shoppingCart.getNumber() - 1);
        shoppingCartMapper.updateByNumber(shoppingCart);
    }
}















